package com.meitianhui.convenitenbuy.module.store.presenter;

import android.os.Bundle;
import android.text.TextUtils;

import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.route.BikingRouteResult;
import com.baidu.mapapi.search.route.DrivingRouteResult;
import com.baidu.mapapi.search.route.IndoorRouteResult;
import com.baidu.mapapi.search.route.MassTransitRouteResult;
import com.baidu.mapapi.search.route.OnGetRoutePlanResultListener;
import com.baidu.mapapi.search.route.PlanNode;
import com.baidu.mapapi.search.route.RoutePlanSearch;
import com.baidu.mapapi.search.route.TransitRouteResult;
import com.baidu.mapapi.search.route.WalkingRoutePlanOption;
import com.baidu.mapapi.search.route.WalkingRouteResult;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.module.store.contract.StoreMapContract;
import com.meitianhui.convenitenbuy.module.store.model.StoreInfo;
import com.meitianhui.convenitenbuy.module.store.ui.activity.StoreMapActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by amy on 2017/12/1.
 */

public class StoreMapPresenter implements StoreMapContract.Presenter {

    private StoreMapContract.View mView;
    private StoreMapActivity mContext;
    private BitmapDescriptor ff3 = BitmapDescriptorFactory.fromResource(R.drawable.icon_gcoding);
    private RoutePlanSearch mSearch;
    private boolean isfirstsearch = true;//是否第一次搜索路线

    public StoreMapPresenter(StoreMapActivity activity, StoreMapActivity context) {
        this.mView = activity;
        this.mContext = context;
    }

    @Override
    public void attachView(Object view) {
    }

    @Override
    public void detachView() {
    }

    @Override
    public void parseAllStore2Map(List<StoreInfo.Convenient> convientStoreList) {
        if (convientStoreList != null && convientStoreList.size() != 0) {
            List<LatLng> latLngList = new ArrayList<>();

            for (StoreInfo.Convenient convientStoreBean : convientStoreList) {
                LatLng latLng = new LatLng(
                        Double.valueOf(convientStoreBean.getLatitude()),
                        Double.valueOf(convientStoreBean.getLongitude()));
                latLngList.add(latLng);
            }
            if (latLngList.size() > 0) {
                for (int i = 0; i < latLngList.size(); i++) {
                    MarkerOptions option = new MarkerOptions().icon(ff3).position(latLngList.get(i));
                    Bundle des = new Bundle();
                    if (convientStoreList.get(i).getStores_name() == null
                            || convientStoreList.get(i).getStores_name().equals("")) {
                        des.putString("des", "没有备注");
                    } else {
                        des.putString("des", convientStoreList.get(i).getStores_name() + ","
                                + convientStoreList.get(i).getAddress() + "," + i);
                    }
                    option.extraInfo(des);
                    mView.parseStore2MapSucc(option);
                }
            }
        }
    }

    @Override
    public void parseStore2Map(StoreInfo.Convenient convientStoreList) {
        if (!TextUtils.isEmpty(convientStoreList.getLatitude())
                && !TextUtils.isEmpty(convientStoreList.getLongitude())) {
            // 初始化搜索模块，注册事件监听
            mSearch = RoutePlanSearch.newInstance();
            mSearch.setOnGetRoutePlanResultListener(new OnGetRoutePlanResultListener() {
                @Override
                public void onGetWalkingRouteResult(WalkingRouteResult result) {
                    if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
                        mView.showNotFind("抱歉，未找到结果");
                        return;
                    }
                    if (result.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) {
                        // 起终点或途经点地址有岐义，通过以下接口获取建议查询信息
                        // result.getSuggestAddrInfo()
                        return;
                    }
                    if (result.error == SearchResult.ERRORNO.NO_ERROR) {
                        if (isfirstsearch) {
                            isfirstsearch = false;
                        } else {
                            mView.clearBaiduMap();
                        }
                        mView.getOverlay(result);
                    }
                }

                @Override
                public void onGetTransitRouteResult(TransitRouteResult transitRouteResult) {
                }

                @Override
                public void onGetMassTransitRouteResult(MassTransitRouteResult massTransitRouteResult) {
                }

                @Override
                public void onGetDrivingRouteResult(DrivingRouteResult drivingRouteResult) {
                }

                @Override
                public void onGetIndoorRouteResult(IndoorRouteResult indoorRouteResult) {
                }

                @Override
                public void onGetBikingRouteResult(BikingRouteResult bikingRouteResult) {
                }
            });

            Double Mylongitude = 0.0;
            Double Mylatitude = 0.0;
            try {
                Mylongitude = Double.parseDouble(InfoProvider.getLongitude(mContext));
                Mylatitude = Double.parseDouble(InfoProvider.getLatitude(mContext));
            } catch (Exception e) {
                e.printStackTrace();
            }
            PlanNode stNode = PlanNode.withLocation(new LatLng(Mylatitude, Mylongitude));
            LatLng latLng = new LatLng(
                    Double.valueOf(convientStoreList.getLatitude()),
                    Double.valueOf(convientStoreList.getLongitude()));
            PlanNode enNode = PlanNode.withLocation(latLng);
            mSearch.walkingSearch((new WalkingRoutePlanOption()).from(stNode).to(enNode));
        }
    }

}
